package web;

import java.sql.*;

public class UserDao {
    public boolean checkLogin(String username, String password){
        Connection connection = null;
        PreparedStatement state = null;
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/kkb_05?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC", "root", "123456");
            state = connection.prepareStatement("select * from user where username=? and password=?");
            state.setString(1,username);
            state.setString(2,password);
            ResultSet rs = state.executeQuery();
            return rs.next();
        } catch(Exception e){
            e.printStackTrace();
        } finally {
            try{
                connection.close();
                state.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        return false;
    }
    public boolean testuname(String uname){
        Connection connection = null;
        PreparedStatement state = null;
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/kkb_05?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC", "root", "123456");
            state = connection.prepareStatement("select * from user where username=?");
            state.setString(1,uname);
            ResultSet rs = state.executeQuery();
            return rs.next();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                connection.close();
                state.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return false;
    }

    public User findUser(String uname){
        Connection connection = null;
        PreparedStatement state = null;
        User user = new User();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/kkb_05?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC", "root", "123456");
            state = connection.prepareStatement("select * from user where username=?");
            state.setString(1,uname);
            ResultSet rs = state.executeQuery();
            while (rs.next()){

            System.out.println("test2");
            System.out.println(rs.getString("username"));
            System.out.println(rs.getString("password"));
            String name = rs.getString("username");
            String pass = rs.getString("password");
            user.setUsername(name);
            user.setPassword(pass);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                connection.close();
                state.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return user;

    }

}

